<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Test Page for geoPolygonUtils.geometry.Line class</title>
  <script language="JavaScript" type="text/javascript"
      src="../tools/jsunit/app/jsUnitCore.js"></script>
  <script language="JavaScript" type="text/javascript"
      src="test-utils.js"></script>
  <script language="JavaScript" type="text/javascript"
      src="../src/geometry.js"></script>
  <script language="JavaScript" type="text/javascript">

    // Alias for the Vector class.
    var Vector = geoPolygonUtils.geometry.Vector;

    
    // Alias for the Line class.
    var Line = geoPolygonUtils.geometry.Line;
    

    function testLineConstructor() {
      // We specifically check that constructor normalizes 'normal' vector.
      var line = new Line(new Vector(-30, 40), -50);
      assertVectorCoords(-0.6, 0.8, line.normal);
      assertAlmostEquals(-1, line.c);
    };
    
    
    function testFromNormalAndPoint() {
      var line = Line.fromNormalAndPoint(new Vector(0, 0), new Vector(6, 1));
      assertNull('line is not created when normal is a zero vector', line);
      
      line = Line.fromNormalAndPoint(new Vector(-30, 40),
                                     new Vector(3, 1));
      assertVectorCoords(-0.6, 0.8, line.normal);
      assertAlmostEquals(1, line.c);
    };
    
    
    function testFromPoints() {
      var line = Line.fromPoints(new Vector(7, 3), new Vector(7, 3));
      assertNull('line is not created when points are equal', line);
      
      line = Line.fromPoints(new Vector(3, 1), new Vector(11, 7));
      assertVectorCoords(-0.6, 0.8, line.normal);
      assertAlmostEquals(1, line.c);
    };
    
    
    function testDistanceToPoint() {
      var line = new Line(new Vector(-3, 4), 5);
      assertAlmostEquals(0, line.distanceToPoint(new Vector(3, 1)));
      assertAlmostEquals(3, line.distanceToPoint(new Vector(10, 10)));
      
      line = Line.fromPoints(new Vector(2, 7), new Vector(5, 11));
      assertAlmostEquals(5, line.distanceToPoint(-2, 10));
    };
    
    
    function testContainsPoint() {
      var line = new Line(new Vector(-3, 4), 5);
      assertTrue(line.containsPoint(new Vector(3, 1)));
      assertFalse(line.containsPoint(new Vector(3, 2)));
      
      line = Line.fromPoints(new Vector(2, 7), new Vector(5, 11));
      assertTrue(line.containsPoint(new Vector(1.99991, 7.00006), 1e-3));
      var precision = new geoPolygonUtils.geometry.Precision(1e-9, 1e-3, 1e-9);
      assertTrue(line.containsPoint(new Vector(1.99991, 7.00006), precision));
    };
    
    
    function testProjectPoint() {
      var line = Line.fromPoints(new Vector(2, 7), new Vector(5, 11));
      assertVectorCoords(2, 7, line.projectPoint(new Vector(-2, 10)));
      assertVectorCoords(2, 7, line.projectPoint(new Vector(6, 4)));
      assertVectorCoords(3.5, 9, line.projectPoint(new Vector(7.5, 6)));
    };
    
    
    function testIntersectLine() {
      var line1 = Line.fromPoints(new Vector(2, 7), new Vector(5, 11));
      var line2 = Line.fromPoints(new Vector(5, 11), new Vector(6, 1));
      var point = line1.intersectLine(line2);
      assertNotNull(point);
      assertVectorCoords(5, 11, point);
      
      line1 = Line.fromPoints(new Vector(3, 1), new Vector(1000000000, 0));
      line2 = Line.fromPoints(new Vector(1000000000, 1), new Vector(3, 1));
      point = line1.intersectLine(line2);
      assertNotNull(point);
      assertVectorCoords(3, 1, point);

      line1 = Line.fromPoints(new Vector(1, 1), new Vector(3, 2));
      line2 = Line.fromPoints(new Vector(-1, 0), new Vector(5, 3));
      point = line1.intersectLine(line2);
      assertNull('equal lines don\'t have a single intersection point', point);
      
      line1 = Line.fromPoints(new Vector(1, 2), new Vector(3, 3));
      line2 = Line.fromPoints(new Vector(-1, 0), new Vector(5, 3));
      point = line1.intersectLine(line2);
      assertNull('parallel lines don\'t intersect', point);
    };
    
    
    function testEquals() {
      line1 = Line.fromPoints(new Vector(1, 1), new Vector(3, 2));
      line2 = Line.fromPoints(new Vector(-1, 0), new Vector(5, 3));
      assertTrue(line1.equals(line2));    

      line1 = Line.fromPoints(new Vector(3, 2), new Vector(1, 1));
      line2 = Line.fromPoints(new Vector(-1, 0), new Vector(5, 3));
      assertTrue(line1.equals(line2));    

      line1 = Line.fromPoints(new Vector(1, 2), new Vector(3, 3));
      line2 = Line.fromPoints(new Vector(-1, 0), new Vector(5, 3));
      assertFalse('parallel non-equal lines', line1.equals(line2));    

      line1 = Line.fromPoints(new Vector(3, 2), new Vector(1, 1.001));
      line2 = Line.fromPoints(new Vector(-1, 0), new Vector(5, 3));
      assertFalse('almost equal lines, but not quite', line1.equals(line2));    
    };

  </script>
  <style>
    .code {
      color: green;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <h1>Test Page for <span class="code">geoPolygonUtils.geometry.Line</span>
  class.</h1>
  <p>This page contains tests for the methods of <span class="code">
  geoPolygonUtils.geometry.Line</span> class. To see them take a look at the
  source of the page.</p>
  <p><em>Note:</em> this file is intended to be used with
  <a href="http://www.jsunit.net/">JSUnit</a> test framework.</p>
</body>
</html>
